#ifndef LHC_CONSOLE_H_INCLUDED_
#define LHC_CONSOLE_H_INCLUDED_

#include "ITextWriter.h"
#include "ITextReader.h"
#include "Macros.h"
#include <boost/noncopyable.hpp>

#ifdef _WIN32
#include <Windows.h>
#include <boost/scoped_ptr.hpp>
#include "FileStream.h"
#endif

namespace lhcutils
{

class ConsoleClass : public ITextWriter, public ITextReader, private boost::noncopyable
{
public:
	ConsoleClass();
	virtual ~ConsoleClass();

	virtual void Write(const UnicodeString& str) OVERRIDE;
	virtual StringPtr ReadLine() OVERRIDE;

private:

#ifdef _WIN32
	HANDLE m_stdoutHandle;
	DWORD m_stdoutFileType;
	bool m_stdoutIsConsole;

	void WriteTrueConsole(const UnicodeString& str);
	void WriteStdoutRedirected(const UnicodeString& str);

	HANDLE m_stdinHandle;
	DWORD m_stdinFileType;
	bool m_stdinIsConsole;
	boost::scoped_ptr<lhcutils::StreamReader> m_stdinRedirectedStream;

	StringPtr ReadLineTrueConsole();
	StringPtr ReadLineStdinRedirected();
#endif
};

extern ConsoleClass Console;

}

#endif

/*
 Copyright 2011 Greg Najda

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
